package com.zx.framework.web.config;

import java.nio.charset.Charset;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.zx.framework.core.util.SpringBeanUtil;
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
@ComponentScan(basePackages = "com.zx.*", useDefaultFilters = false, includeFilters = {  
        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class}),
        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {ControllerAdvice.class})  
})
public class MvcConfig extends WebMvcConfigurationSupport {
	
	private static final Logger LOGGER = Logger.getLogger(MvcConfig.class);

	@Bean
	public ThreadPoolTaskExecutor mvcTaskExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(10);
		executor.setQueueCapacity(100);
		executor.setMaxPoolSize(25);
		return executor;

	}

	@Override
	public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
		 configurer.setDefaultTimeout(30*1000L); //tomcat默认10秒
	     configurer.setTaskExecutor(mvcTaskExecutor());//所借助的TaskExecutor
	}
	
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/async").setViewName("/async");
	}

	@Override
	protected void configureMessageConverters(
			List<HttpMessageConverter<?>> converters) {
		StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
		converters.add(stringHttpMessageConverter);
		
		addDefaultHttpMessageConverters(converters);
	}

	@Override
	public HandlerExceptionResolver handlerExceptionResolver() {
		//处理异常，否则按照自身默认的异常处理，加入此段代码为了xml配置生效
		HandlerExceptionResolver handler = SpringBeanUtil.getBean("handlerExceptionResolver",HandlerExceptionResolver.class);
		if(handler!=null){
			return handler;
		}
		return super.handlerExceptionResolver();
	}
	
//	@Override
//	public void configureViewResolvers(ViewResolverRegistry registry) {
//		registry.viewResolver(freemarkerViewResolver());
//		registry.viewResolver(viewResolver());
//	}
//	
//	
//	@Override
//    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//        configurer.enable();
//    }
//
//	/**
//	 * jsp渲染器
//	 * @return
//	 */
//    public ViewResolver viewResolver() {  
//		LOGGER.info("jspViewResolver");  
//        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();  
//        viewResolver.setPrefix("/WEB-INF/jsp/");
//        viewResolver.setSuffix(".jsp"); 
//        return viewResolver;  
//    }
//	/**
//	 * freemarker渲染器
//	 * @return
//	 */
//    public ViewResolver freemarkerViewResolver() {  
//		LOGGER.info("freemarkerViewResolver");  
//		FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
//        viewResolver.setSuffix(".ftl"); 
//        viewResolver.setContentType("text/html;charset=utf-8");
//        viewResolver.setViewClass(FreeMarkerView.class);
//        return viewResolver;  
//    }
//	/**
//	 * 获取freemarker的配置
//	 * @return
//	 */
//	@Bean
//	public FreeMarkerConfigurer freeMarkerConfigurer(){
//		LOGGER.info("FreeMarkerConfigurer");
//		FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
//		configurer.setDefaultEncoding("UTF-8");
//		configurer.setTemplateLoaderPaths("/WEB-INF/ftl/","/WEB-INF/classes/");
//		Map<String, Object> variables = new HashMap<String, Object>();
//		variables.put("datetime_format", "yyyy-MM-dd HH:mm:ss");
//		variables.put("date_format", "yyyy-MM-dd");
//		variables.put("url_escaping_charset", "UTF-8");
//		variables.put("default_encoding", "UTF-8");
//		configurer.setFreemarkerVariables(variables );
//		return configurer;
//	}
//
//	/**
//	 * 附件上传视图
//	 * @return
//	 */
//	@Bean(name="multipartResolver")
//	public CommonsMultipartResolver commonsMultipartResolver(){
//		CommonsMultipartResolver cr = new CommonsMultipartResolver();
//		cr.setMaxUploadSize(512500);
//		return cr;
//	}
//	
//	@Bean  
//    public MessageSource messageSource() {  
//        LOGGER.info("MessageSource");  
//        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();  
//        messageSource.setBasename("globalMessages");  
//        return messageSource;  
//    }
//	/**
//	 * 静态文件的处理
//	 */
//	@Override
//	public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/static/**").addResourceLocations("/static/").setCachePeriod(31556926);
//    }
//	/**
//	 * 添加自定义拦截器
//	 */
//	@Override
//    public void addInterceptors(InterceptorRegistry registry) {
//		
//		SessionInterceptor sessionInterceptor = new SessionInterceptor();
//		sessionInterceptor.setSessionType("com.zx.framework.plat.user.vo.UserVo");
//        registry.addInterceptor(sessionInterceptor).addPathPatterns("/**")
//        	.excludePathPatterns("/","/login","/login/**","/static/**","/api/**");
//        
////        AuthenInterceptor authenInterceptor = new AuthenInterceptor();
////        registry.addIntercepto         r(authenInterceptor).addPathPatterns("/**")
////    		.excludePathPatterns("/","/login","/login/**","/static/**","/api/**");
//        
//    }
//
//
//	@Override
//	public HandlerExceptionResolver handlerExceptionResolver() {
//		
//		return super.handlerExceptionResolver();
//	}
//	
	
	
}
